﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;
using Koonsoft.HYCRM.Model.Enum;

public partial class Empolyee_DistributionEmplyeeContract : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["ID"] != null)
            {
                BindEmpolyeeData();
            }
        }

    }
    private void BindEmpolyeeData()
    {
        int empolyeeID = Convert.ToInt32(Request.QueryString["ID"]);

        var empolyee = new EmpolyeeDAL().GetEmpolyeeInfoByEmpolyeeID(empolyeeID);

        if (empolyee != null)
        {
            EmpolyeeName.Text = empolyee.Name;
            LoginName.Text = empolyee.LoginName;
            DropDownList1.SelectedValue = empolyee.RoleID.ToString();
            Barcode.Text = empolyee.Barcode;
            txtEntryTime.Text = empolyee.EntryTime.ToString("yyyy-MM-dd");
        }
    }
    protected void BindBranch(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null)
        {
            int empolyeeID = Convert.ToInt32(Request.QueryString["ID"]);

            var branchid = new EmpolyeeBLL().GetBranchesByEmpolyeeID(empolyeeID);
            var contracts = new EmpolyeeBLL().GetEmplyeeContractByEmplyeeId(empolyeeID);

            if (branchid != null && branchid.Count > 0)
            {
                foreach (ListItem item in CheckBoxList1.Items)
                {
                    if (branchid.Contains(Convert.ToInt32(item.Value)))
                    {
                        item.Selected = true;
                        continue;
                    }

                    item.Selected = false;
                }
            }

            GridView1.DataSource = contracts;
            GridView1.DataBind();
        }
    }

    protected void SaveData_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null)
        {
            List<int> list = new List<int>();

            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    list.Add(Convert.ToInt32(item.Value));
                }
            }
            
            DateTime entryTime= new DateTime();

            try
            {
                entryTime = DateTime.Parse(txtEntryTime.Text.Trim());
            }
            catch (Exception)
            {
                AlertMsg("入职时间式错误");
                return;
            }


            var empolyeeID = Convert.ToInt32(Request.QueryString["ID"]);

            new EmpolyeeBLL().UpdateEmpolyeeInfoByEmpolyeeID(EmpolyeeName.Text.Trim(), LoginName.Text.Trim(),
                                                      Page.User.Identity.Name, Barcode.Text.Trim(), empolyeeID, entryTime
                                                      , Convert.ToInt32(DropDownList1.SelectedValue), list);


            AlertMsg("保存成功");
        }
    }

    /// <summary>
    /// 行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            int status = Convert.ToInt32(e.Row.Cells[3].Text);
            EmplyeeContractStatus pStatus = (EmplyeeContractStatus)status;
            e.Row.Cells[3].Text = RemarkAttribute.GetEnumRemark(pStatus);
        }
    }
}